<template>
	<view class="goods-wrap">
		<goods-list :goodsList="goodsList" @clickGoodsItem="goGoodsDetail"></goods-list>
		<view class="bottom-line" v-if="flag">
			------ 我是有底线的 ------
		</view>
	</view>
</template>

<script>
	import { reqGoodsList } from '@/api'
	import goodsList from '@/components/goods-list/goods-list'
	export default {
		data() {
			return {
				goodsList: [],
				pageNum: 1, // 商品页码
				total: 0, // 总页码
				flag: false, // 是否显示底线 
			};
		},
		components: {
			'goods-list': goodsList
		},
		onLoad() {
			this.getGoodsList(1)
		},
		methods: {
			// 获取商品列表
			async getGoodsList(pageNum, callBack) {
				const result = await reqGoodsList(pageNum)
				if(result.code !== 200) {
					return uni.showToast({
						title:'数据获取失败！'
					})
				}
				this.total = result.total
				this.goodsList = [...this.goodsList, ...result.data]
				// 去掉下拉刷新loading
				callBack && callBack()
			},
			// 跳转到详情页
			goGoodsDetail(id) {
				uni.navigateTo({
					url: `/pages/goods/goods-detail?id=${id}`
				})
			}
		},
		// 滑到底部时
		onReachBottom() {
			// console.log('触底了')
			if(this.goodsList.length < this.total) {
				this.pageNum++
				this.getGoodsList(this.pageNum)
			} else {
				return this.flag = true
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			// console.log('下拉刷新了')
			this.pageNum = 1
			this.goodsList = []
			this.flag = false
			setTimeout(() => {
				this.getGoodsList(1, () => {
					// 关闭下拉刷新
					uni.stopPullDownRefresh()
				})
			},1000)
		}
	}
</script>

<style lang="scss">
	.goods-wrap {
		background-color: #eee;
	}
	.bottom-line {
		width: 100%;
		height: 50px;
		line-height: 50px;
		text-align: center;
		font-size: 28rpx;
	}
</style>
